const baseURL = 'https://zg99.offcn.com/index/'

function getwxphone(code, actid, leixing, info) {
  let area = info.area;
  let agent = info.agent;
  let page = info.page;
  let activeId = info.activeId;

  return new Promise(function (resolve, reject) {
    wx.request({
      url: baseURL + 'wechat/getwxphone?actid=' + actid,
      data: {
        code
      },
      success(res) {
        if (res.data.status == 1) {
          var phone = res.data.phone; //获取得到手机号
          console.log("手机号=", phone)
          wx.setStorageSync(actid + 'phone', phone)
          wx.request({ //发送请求
            url: baseURL + leixing + '/register?actid=' + actid, //接收url
            data: {
              phone,
              area,
              agent,
              isagree: true
            }, //接收参数
            success(res) {
              var jsonStr = res.data;
              jsonStr = jsonStr.replace("(", "");
              jsonStr = jsonStr.replace(")", "");
              if (typeof jsonStr != 'object') {
                jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                var theJson = JSON.parse(jsonStr);
              }
              console.log(theJson)
              resolve(theJson);
            }
          })
          wx.request({
            url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/collect_v3',
            method: 'POST',
            data: {
              activeId: activeId,
              mobile: phone,
              areaCode: area,
              agent: agent,
              pageId: page
            }
          })
        } else {
          console.log("获取手机号失败 ", res.data.msg)
        }
      },
      fail(err) {
        reject(err)
        console.log(err);
      }
    })
  })
}

module.exports = getwxphone